package com.caul.demo.hadoop.mapreduce.rank;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * Created by sdliang on 2018/4/11.
 */
public class RankReducer extends Reducer<RankKey, NullWritable, RankKey, NullWritable> {

  int TOPN = 0;

  @Override
  protected void setup(Context context) throws IOException, InterruptedException {
    TOPN = context.getConfiguration().getInt("top.n", 5);
  }

  @Override
  protected void reduce(RankKey key, Iterable<NullWritable> values, Context context)
      throws IOException, InterruptedException {
    int count = 0;
    for (NullWritable value : values) {
      if (count++ > TOPN) {
        return;
      }
      context.write(key, NullWritable.get());
    }
  }
}
